<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Admin extends MY_Controller {
	var $main_dir;
	
	function __construct() {
		parent::__construct();
		$this->main_dir = dirname(dirname(dirname(dirname(__FILE__))));
	}
	
	function load_import_scheduler_modal($data = array('init_modal' => FALSE)) {
		if (!isset($_COOKIE['import_scheduler_data'])) {
			$import_scheduler_data = array('task' => 'upload', 'progress' => 0);
			set_json_cookie('import_scheduler_data', $import_scheduler_data);
			
			$data['init_modal'] = TRUE;
		}
		//var_dump($data);
		$this->load->view('module/index/import_scheduler_modal_body', $data);
	}
	
	function import_scheduler_upload() {
		$file_name = $_FILES['uploadedFile']['name'];
		if (move_uploaded_file($_FILES['uploadedFile']['tmp_name'], $this->main_dir . '\upload\\' . $file_name)) {
			$import_scheduler_data = array('uploadFileName' => $file_name, 'task' => 'upload', 'progress' => 100);
			set_json_cookie('import_scheduler_data', $import_scheduler_data);
		}
	}

	function import_scheduler_generate($clear_current_scheduler_date_related_datas = FALSE) {
		//ini_set('memory_limit', '512M');
		ini_set('max_execution_time', '600');
		// set output_buffering in php.ini or you can add the line 
		// php_value output_buffering "0"/ Off
		// to your .htaccess file. It will not work with ini_set() since it is PHP_INI_PERDIR.
		
		$this->load->model('const_airplane_model', 'airplane_model_m');
		$this->load->model('const_airport', 'airport_m');
		$this->load->model('f_flight_scheduler_date', 'flight_scheduler_date_m');
		$this->load->model('f_flight_scheduler', 'flight_scheduler_m');
		$this->load->model('f_flight', 'flight_m');
		$this->load->model('f_flight_scheduler_price', 'flight_scheduler_price_m');
		
		require_once $this->main_dir . '\plugin\phpexcel\Classes\PHPExcel.php';
		
		$import_scheduler_data = get_json_cookie('import_scheduler_data');
		$inputFilePath = $this->main_dir . '\upload\\' . $import_scheduler_data['uploadFileName'];
		
		$objReader = new PHPExcel_Reader_Excel2007();
		$objPHPExcel = $objReader->load($inputFilePath);
		
		/* For worksheet: Scheduler Date */
		$objPHPExcel->setActiveSheetIndexByName('Scheduler Date');
		$objWorksheet = $objPHPExcel->getActiveSheet();
		
		$arrWorksheet = $objWorksheet->toArray(null, true, true, true);
		
		$scheduler_date_temp = $this->flight_scheduler_date_m->search_by_condition(array('start_date' => $arrWorksheet[3]['A'], 'end_date' => $arrWorksheet[3]['B']));
		if (empty($scheduler_date_temp)) {
			$scheduler_date_id = $this->flight_scheduler_date_m->save(array('start_date' => $arrWorksheet[3]['A'], 'end_date' => $arrWorksheet[3]['B']));
		} else {
			if ($clear_current_scheduler_date_related_datas == FALSE) {
				echo 'The scheduler date is already exist';
				return;
			} else {
				echo 'Start clearing...';
				$to_clear_scheduler_ids = $this->flight_scheduler_m->get_by('scheduler_date_id', $scheduler_date_temp['id'], 'id', TRUE);
				foreach ($to_clear_scheduler_ids as $to_clear_scheduler_id) {
					$this->flight_m->delete_by('flight_scheduler_id', $to_clear_scheduler_id);
				}
				
				$this->flight_scheduler_m->delete_by('scheduler_date_id', $scheduler_date_temp['id']);
				$this->flight_scheduler_price_m->delete_by('scheduler_date_id', $scheduler_date_temp['id']);
				
				$scheduler_date_id = $scheduler_date_temp['id'];
			}
		}
		
		
		/* For worksheet: Scheduler */
		$objPHPExcel->setActiveSheetIndexByName('Scheduler');
		$objWorksheet = $objPHPExcel->getActiveSheet();
		
		$objWorksheet->getStyle('E1:F' . $objWorksheet->getHighestRow())->getNumberFormat()->setFormatCode('H:i:s');
		
		$arrWorksheet = $objWorksheet->toArray(null, true, true, true);
		
		ob_start();
		echo str_repeat(' ', 1024);
		$i = 0;
		foreach ($arrWorksheet as $k => $v) {
			if ($k >= 3) {
				$scheduler_update = array(
						'flight_code' => $v['A'],
						'scheduler_date_id' => $scheduler_date_id,
						'airplane_model_id' => $this->airplane_model_m->get_by('model_code', $v['B'], 'id', TRUE),
						'from_airport_id' => $this->airport_m->get_by('airport_code', $v['C'], 'id', TRUE),
						'to_airport_id' => $this->airport_m->get_by('airport_code', $v['D'], 'id', TRUE),
						'departure_time' => $v['E'],
						'arrival_time' => $v['F'],
						'scheduler' => $v['G']
						);
				$scheduler_id = $this->flight_scheduler_m->save($scheduler_update);
				
				if ($this->flight_m->generate_flights_by_scheduler_id($scheduler_id) == 1) {
					$i ++;
					
					echo
						"<html><script>$(function() {" .
						"$('.generate').find('.bar').css({ 'width': '" . (($i / (count($arrWorksheet) - 2)) * 100) . "%' });" .
						"})</script></html>";
					
					ob_flush();
					flush();
					
					sleep(1);
				};
			}
		}
		
		echo 'generate_succeed';
		ob_flush();
		flush();
		
		/* For worksheet: Scheduler Price */
		$objPHPExcel->setActiveSheetIndexByName('Scheduler Price');
		$objWorksheet = $objPHPExcel->getActiveSheet();
		
		$arrWorksheet = $objWorksheet->toArray(null, true, true, true);
		
		foreach ($arrWorksheet as $k => $v) {
			if ($k >= 3) {
				$scheduler_price_update = array(
						'scheduler_date_id' => $scheduler_date_id,
						'from_airport_id' => $this->airport_m->get_by('airport_code', $v['A'], 'id', TRUE),
						'to_airport_id' => $this->airport_m->get_by('airport_code', $v['B'], 'id', TRUE),
						'economy_jeju_discount_price' => $v['C'],
						'economy_special_price' => $v['D'],
						'economy_basic_price' => $v['E'],
						'economy_season_price' => $v['F'],
						'prestige_special_price' => $v['G'],
						'prestige_basic_price' => $v['H'],
						'prestige_season_price' => $v['I']
				);
				$this->flight_scheduler_price_m->save($scheduler_price_update);
			}
		}
	}
}